import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect: '/home',  // 重定向到主页
    children: [
      { path: '403', name: 'NoAuth', meta: { name: '无权限' }, component: () => import('../views/manager/403') },
      { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/manager/Home') },
      { path: 'admin', name: 'Admin', meta: { name: '管理员信息' }, component: () => import('../views/manager/Admin') },
      { path: 'adminPerson', name: 'AdminPerson', meta: { name: '个人信息' }, component: () => import('../views/manager/AdminPerson') },
      { path: 'password', name: 'Password', meta: { name: '修改密码' }, component: () => import('../views/manager/Password') },
      { path: 'labAdmin', name: 'LabAdmin', meta: { name: '实验员信息' }, component: () => import('../views/manager/LabAdmin') },
      { path: 'teacher', name: 'Teacher', meta: { name: '教师信息' }, component: () => import('../views/manager/Teacher') },
      { path: 'student', name: 'Student', meta: { name: '学生信息' }, component: () => import('../views/manager/Student') },
      { path: 'labAdminPerson', name: 'LabAdminPerson', meta: { name: '个人信息' }, component: () => import('../views/manager/LabAdminPerson') },
      { path: 'teacherPerson', name: 'TeacherPerson', meta: { name: '个人信息' }, component: () => import('../views/manager/TeacherPerson') },
      { path: 'studentPerson', name: 'StudentPerson', meta: { name: '个人信息' }, component: () => import('../views/manager/StudentPerson') },
      { path: 'semester', name: 'Semester', meta: { name: '学期信息' }, component: () => import('../views/manager/Semester') },
      { path: 'lab', name: 'Lab', meta: { name: '实验室信息' }, component: () => import('../views/manager/Lab') },

      { path: 'schedule', name: 'Schedule', meta: { name: '实验课申请登记' }, component: () => import('../views/manager/Schedule') },
      { path: 'repair', name: 'Repair', meta: { name: '实验室设备报修' }, component: () => import('../views/manager/Repair.vue') },

      { path: 'borrow', name: 'Borrow', meta: { name: '实验室借用申请' }, component: () => import('../views/manager/Borrow') },
      { path: 'doBorrow', name: 'DoBorrow', meta: { name: '实验室借用审批' }, component: () => import('../views/manager/DoBorrow') },
      { path: 'doSchedule', name: 'DoSchedule', meta: { name: '实验室排课审批' }, component: () => import('../views/manager/DoSchedule') },
      { path: 'doRepair', name: 'DoRepair', meta: { name: '设备报修处理' }, component: () => import('../views/manager/DoRepair') },


    ]
  },
  {
    path: '/front',
    name: 'Front',
    component: () => import('../views/Front.vue'),
    children: [
      { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/front/Home') },
      { path: 'person', name: 'Person', meta: { name: '个人信息' }, component: () => import('../views/front/Person') },
    ]
  },
  { path: '/login', name: 'Login', meta: { name: '登录' }, component: () => import('../views/Login.vue') },
  { path: '*', name: 'NotFound', meta: { name: '无法访问' }, component: () => import('../views/404.vue') },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 注：不需要前台的项目，可以注释掉该路由守卫
// 路由守卫
// router.beforeEach((to ,from, next) => {
//   let user = JSON.parse(localStorage.getItem("xm-user") || '{}');
//   if (to.path === '/') {
//     if (user.role) {
//       if (user.role === 'USER') {
//         next('/front/home')
//       } else {
//         next('/home')
//       }
//     } else {
//       next('/login')
//     }
//   } else {
//     next()
//   }
// })

export default router
